#!/usr/bin/env python
# -*- coding: utf-8 -*-

from odoo import models, fields, _
from odoo.exceptions import UserError, ValidationError
 
class StockMove(models.Model):
    _inherit = 'stock.move' 
 
    batch_id = fields.Many2one('expired.quality.batch',  string='质检批次')
    quality_state = fields.Selection([
        ('pass', '通过'),
        ('fail', '失败')], string='质检结果')

    def open_move_line_view(self):
        pass

    def _action_done(self, cancel_backorder=False):
        if self.picking_type_id.code == 'expired_quality' \
                and self.batch_id \
                and self.batch_id.quality_check_ids.filtered(lambda x:x.quality_state == 'none'):
            raise ValidationError(
                _("发现未完成的到期品检单：{text}").format(
                    text='/'.join(
                        self.batch_id.quality_check_ids.filtered(
                            lambda x:x.quality_state == 'none'
                        ).mapped('name')
                    )
                ))
        res = super(StockMove, self)._action_done(cancel_backorder)
        return res
